package com.ziyun.erp.web.api.order;

import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.modules.order.entity.UserAccessoryEntity;
import com.ziyun.erp.modules.order.vo.UserAccessoryVo;
import com.ziyun.erp.modules.order.service.UserAccessoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * 我的附件
 *
 * @author gaosheng
 * @email
 * @date 2018-09-01 10:02:31
 */
@RestController
@RequestMapping("port/order/useraccessory")
public class UserAccessoryControllerApi {

	@Autowired
	private UserAccessoryService userAccessoryService;

	@Value("${oss.host}")
	private String ossHost;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("order:useraccessory:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = userAccessoryService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("order:useraccessory:info")
	public R info(@PathVariable("id") Integer id) {
		UserAccessoryEntity userAccessory = userAccessoryService.selectById(id);

		return R.ok().put("userAccessory", userAccessory);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("order:useraccessory:update")
	public R update(@RequestBody UserAccessoryEntity userAccessory) {
		ValidatorUtils.validateEntity(userAccessory);
		userAccessoryService.updateAllColumnById(userAccessory);// 全部更新

		return R.ok();
	}

	/**
	 * 查询附件
	 * 
	 * @param
	 *
	 * @return
	 */
	@RequestMapping("/userAccessorys")
	public R userAccessorys( @RequestBody Map<String, Object> paramBodyMap) {
		String itemId=paramBodyMap.get("itemId").toString();
		List<UserAccessoryVo> list = userAccessoryService.queryListByItemId(itemId);
		return R.ok().put("list", list);
	}


	/**
	 * @author   Alix_zw
	 * @Date
	 * @Description   获取文件列表
	 
	 * @Return    
	**/
	@RequestMapping("/userAccessorysByOrderNo")
	public R userAccessoryss(@RequestBody Map<String,Object> map){
		String orderNo = map.get("orderNo").toString();
		List<UserAccessoryVo> list = userAccessoryService.queryListByOrderNo(orderNo);
		return R.ok().put("list", list);
	}

	/**
	 * 添加附件
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping("/save")
	public R save(@RequestBody Map<String, Object> paramBodyMap) {
		UserAccessoryVo userAccessoryVo=JsonUtils.string2Object(paramBodyMap.get("userAccessoryVo").toString(),UserAccessoryVo.class);
		Long userId = Long.valueOf(paramBodyMap.get("userId").toString());
		userAccessoryService.saveUserAccessor(userAccessoryVo,userId);
		return R.ok();
	}

	/**
	 * 删除附件
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Map<String, Object> paramBodyMap) {
		Integer[] ids=JsonUtils.string2Array(paramBodyMap.get("ids").toString(),Integer.class);
		userAccessoryService.deleteUserAccessor(ids);
		return R.ok();
	}

	/**
	 * 附件下载
	 * 
	 * @param
	 * @param request
	 * @param response
	 */
	@RequestMapping("/download")
	public R uploadAccessorys(@RequestBody Map<String, Object> paramBodyMap, HttpServletRequest request,
			HttpServletResponse response) {
		Integer id=Integer.valueOf(paramBodyMap.get("id").toString());
		UserAccessoryEntity userAccessory = userAccessoryService.selectById(id);
		String accessoryUrl = ossHost + "/" + userAccessory.getUrl();
		return R.ok().put("accessoryUrl",accessoryUrl).put("name",userAccessory.getName());
	}

}
